package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysRoleMapper;
import com.atguigu.spzx.manager.mapper.SysRoleMenuMapper;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.entity.sys.SysRole;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    public PageInfo<SysRole> list(Integer pageNum, Integer pageSize, String keyword) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //条件查询
        List<SysRole> list = sysRoleMapper.selectByKeyword(keyword);
        return new PageInfo<SysRole>(list);
    }

    @Override
    public void save(SysRole sysRole) {
        sysRoleMapper.save(sysRole);
    }

    @Override
    public void deleteByIds(List<Long> asList) {
        sysRoleMapper.deleteByIds(asList);
    }

    @Override
    public SysRole getById(Long id) {
        return sysRoleMapper.selectById(id);
    }

    @Override
    public void update(SysRole sysRole) {
        sysRoleMapper.updateById(sysRole);
    }

    @Override
    public List<SysRole> list() {
        return sysRoleMapper.selectByKeyword(null);
    }

    @Override
    public List<SysRole> listAllRolesAndIsChecked(Long userId) {

        return sysRoleMapper.selectAllRolesAndIsChecked(userId);
    }

    @Override
    public void assignMenus2Role(Long id, List<Long> menuIds) {
        //1、删除id的角色已分配的所有的菜单
        sysRoleMenuMapper.deleteByRoleId(id);
        //2、如果本次提交的菜单id不为空 分配给id的角色
        if(CollectionUtils.isEmpty(menuIds)){
            return;
        }
        sysRoleMenuMapper.assignMenus2Role(id , menuIds);
    }
}
